package com.chenjt.juc.eight_CAS;

import java.util.concurrent.atomic.AtomicReference;

/**
 * 注释：
 *
 * @author chenjt
 * @date 2024/7/11 10:19
 */
class User{
    String userName;
    int age;

    public User(String userName,int age){
        this.userName = userName;
        this.age = age;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "userName='" + userName + '\'' +
                ", age=" + age +
                '}';
    }
}



public class AtomicReferenceDemo {

    public static void main(String[] args) {
        AtomicReference<User> atomicReference = new AtomicReference<>();

        User z3 = new User("z3", 22);
        User li4 = new User("li4", 28);

        atomicReference.set(z3);

        System.out.println(atomicReference.get());

        //atomicReference 里存储的值预期是 z3，是就修改成li4，不是就不动
        System.out.println(atomicReference.compareAndSet(z3,li4) + "\t" + atomicReference.get());
        System.out.println(atomicReference.compareAndSet(z3,li4) + "\t" + atomicReference.get());

    }


}


